package com.macrounion.atv.utils;

import java.util.List;

import com.google.common.collect.Lists;

public class CommandUtil {
	private List<String> stdoutList = Lists.newArrayList();
	private List<String> erroroutList = Lists.newArrayList();
	private String charset = "GBK";
	
	
	public List<String> getStdoutList() {
		return stdoutList;
	}

	public void setStdoutList(List<String> stdoutList) {
		this.stdoutList = stdoutList;
	}

	public List<String> getErroroutList() {
		return erroroutList;
	}

	public void setErroroutList(List<String> erroroutList) {
		this.erroroutList = erroroutList;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

	public boolean execCommand(String... commands) {
		return execCommand(Lists.newArrayList(commands));
	}

	public boolean execCommand(List<String> commands) {
		try {
			//ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", "net start | findstr /i mysqla");
			ProcessBuilder processBuilder = new ProcessBuilder(commands);
			Process process = processBuilder.start();
			ReadThread stdoutThread = new ReadThread(process.getInputStream(), charset, stdoutList);
			ReadThread erroroutThread = new ReadThread(process.getErrorStream(), charset, erroroutList);
			stdoutThread.start();
			erroroutThread.start();
			process.waitFor();
			return process.exitValue() == 0;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return false;
	}
}
